/**
 * 
 */
package in.smartbillpay.web.dao;

import in.smartbillpay.web.models.City;
import in.smartbillpay.web.models.District;
import in.smartbillpay.web.models.State;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ADIMBLE
 *
 */
@Repository
@Transactional(readOnly = true)
public class LocationDaoImpl implements LocationDao {

	 protected HibernateTemplate template;

	    /**
	     * Sets Hibernate session factory.
	     */
	 @Autowired
	    public void setSessionFactory(SessionFactory sessionFactory) {
	        template = new HibernateTemplate(sessionFactory);
	    }
	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getCityById(int)
	 */
	public City getCityById(int id) {
		return template.get(City.class,id);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getDistrictById(int)
	 */
	public District getDistrictById(int id) {
		return template.get(District.class,id);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getStateById(int)
	 */
	public State getStateById(int id) {
		/*String query = "from State where id=?";
		return (State)template.find(query,id).get(0); */
		return template.get(State.class,id);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getAllCities()
	 */
	public List<City> getAllCities() {
		return template.loadAll(City.class);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getAllDistricts()
	 */
	public List<District> getAllDistricts() {
		return template.loadAll(District.class);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#getAllState()
	 */
	public List<State> getAllState() {
		return template.loadAll(State.class);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#saveOrUpdate(in.smartbillpay.web.models.City)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public City saveOrUpdate(City city) {
		template.saveOrUpdate(city);
		return city;
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#saveOrUpdate(in.smartbillpay.web.models.District)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public District saveOrUpdate(District district) {
		template.saveOrUpdate(district);
		return district;
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#saveOrUpdate(in.smartbillpay.web.models.State)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public State saveOrUpdate(State state) {
		template.saveOrUpdate(state);
		return state;
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#delete(in.smartbillpay.web.models.City)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public void delete(City city) {
		template.delete(city);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#delete(in.smartbillpay.web.models.District)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public void delete(District district) {
		template.delete(district);
	}

	/* (non-Javadoc)
	 * @see in.smartbillpay.web.dao.LocationDao#delete(in.smartbillpay.web.models.State)
	 */
	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public void delete(State state) {
		template.delete(state);

	}
	@SuppressWarnings("unchecked")
	public List<City> getCities(int districtId) {
		String query = "from City where DistrictId=?";
		return template.find(query,districtId);
	}
	@SuppressWarnings("unchecked")
	public List<District> getDistricts(int stateId) {
		String query = "from District where stateId=?";
		return template.find(query,stateId);
	}

}
